package GUILayer;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.LayoutManager;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import ControlLayer.OrderCtr;
import ControlLayer.ProductCtr;
import ModelLayer.Product;
import ModelLayer.ProductLine;
import ModelLayer.SalesOrder;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ProductLineMenu {
	private static ProductLineMenu instance;
	
	public JFrame frame;
	private JTextField txtProductname;
	private JTextField txtQuantity;
	private JTextField txtOrderid;
	private JTextField txtPrice;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ProductLineMenu window = new ProductLineMenu();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	private ProductLineMenu() {
		initialize();
	}

	public static ProductLineMenu getInstance()
	{
		if(instance==null)
		{
			instance = new ProductLineMenu();
		}
		return instance;
	}
	
	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JLabel lblProduct = new JLabel("Product:");
		lblProduct.setBounds(10, 69, 86, 14);
		frame.getContentPane().add(lblProduct);
		
		JLabel lblQuantity = new JLabel("Quantity:");
		lblQuantity.setBounds(136, 69, 59, 14);
		frame.getContentPane().add(lblQuantity);
		
		txtProductname = new JTextField();
		txtProductname.setBounds(10, 94, 86, 20);
		frame.getContentPane().add(txtProductname);
		txtProductname.setColumns(10);
		
		txtQuantity = new JTextField();
		txtQuantity.setBounds(136, 94, 59, 20);
		frame.getContentPane().add(txtQuantity);
		txtQuantity.setColumns(10);
		
		JLabel lblOrderId = new JLabel("Order ID:");
		lblOrderId.setBounds(10, 26, 86, 14);
		frame.getContentPane().add(lblOrderId);
		
		txtOrderid = new JTextField();
		txtOrderid.setBounds(122, 23, 86, 20);
		frame.getContentPane().add(txtOrderid);
		txtOrderid.setColumns(10);
		
		JLabel lblPrice = new JLabel("Price:");
		lblPrice.setBounds(231, 69, 46, 14);
		frame.getContentPane().add(lblPrice);
		
		txtPrice = new JTextField();
		txtPrice.setBounds(231, 94, 86, 20);
		frame.getContentPane().add(txtPrice);
		txtPrice.setColumns(10);
		
		JButton btnAddToOrder = new JButton("Add to order");
		btnAddToOrder.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				OrderCtr orderCtr = new OrderCtr();
				int orderId = Integer.parseInt(txtOrderid.getText());
				String productName = txtProductname.getText();
				int quantity = Integer.parseInt(txtQuantity.getText());
				SalesOrder order = checkOrder(orderId);
				Product product = checkProduct(productName);
				if(order != null && product != null)
				{
					txtPrice.setText(String.valueOf(product.getSalesPrice()*quantity));
					float price = Float.parseFloat(txtPrice.getText());
					orderCtr.createNewProductLine(product.getId(), quantity, price, order.getId());					
				}
			}
		});
		btnAddToOrder.setBounds(10, 159, 100, 23);
		frame.getContentPane().add(btnAddToOrder);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				frame.dispose();
			}
		});
		btnCancel.setBounds(182, 159, 89, 23);
		frame.getContentPane().add(btnCancel);
	}
	
	private SalesOrder checkOrder(int orderId)
	{
		OrderCtr orderCtr = new OrderCtr();
		return orderCtr.findOrderById(orderId);
	}
	
	private Product checkProduct(String name)
	{
		ProductCtr productCtr= new ProductCtr();
		return productCtr.searchByName(name);
	}
}
